
// in loc de new Bomb , pentru performanta as putea
// sa fac la init vreo 5 bombe, si sa le fac sa apara si sa explodeze si sa dispara
// dupa cum e nevoie
var Bomb = new Class({
    Extends: DynaObject,
    initialize: function(x,y,hero){
        this.parent(GAMEUNITTYPE.BOMB);
        this.bombExplosionLength=game.bombDefaultExplosionLength;
        var alignedX=x+game.standardWidth/2;
        var alignedY=y+game.standardHeight/2;
        if(alignedX%game.standardWidth!=0)
            alignedX=util.findMaximumDivider(alignedX,game.standardWidth);
        if(alignedY%game.standardHeight!=0)
            alignedY=util.findMaximumDivider(alignedY,game.standardHeight);
        this.setPosition(alignedX,alignedY);
        if(headless){
        // java code to wait game.bombTimeOut untill it wil call a JS Method back to JSEngine and explode the bomb
        } else
            this.cancelFlag=this.explode.delay(game.bombTimeOut,this);
        this.hero=hero;
        game.activeBombs.push(this);
        if(!headless){
            var soundSuffix=4+Math.round(Math.random()*2);
            game.sound.playSound('bomb'+soundSuffix);
        }
    },
    setExplosionLength: function(explosionLength){
        this.bombExplosionLength = explosionLength;
    },
    canExplodeIntoZone: function(zone){
        return (zone.freeZone || (!zone.freeZone && !zone.hasType(GAMEUNITTYPE.WALL)));
    },
    explode: function(){
        if(!headless){
            //this.HTMLIncarnation.style.backgroundColor='#ff0000';//this.HTMLIncarnation.setStyle('background-color','#ff0000');
            this.HTMLIncarnation.style.backgroundImage="url('img/bomb-explosion2.png')";
        }
        this.explodedZones = [];
        this.oldX=this.x;
        this.oldY=this.y;
        this.newX=this.x;this.newY=this.y;this.newWidth=game.standardWidth;this.newHeight=game.standardHeight;
        var stopMovingLeft=false;var stopMovingRight=false;var stopMovingTop=false;var stopMovingBottom=false;
        for(var i=0;i<this.bombExplosionLength;i++){
                
            var movedLeft=false;
            var movedUp=false;
                
            if(!stopMovingLeft && this.newX-game.standardWidth/2>0){
                var leftZone = util.getZoneAtPoint(this.newX-game.standardWidth/2,this.y);
                if(this.canExplodeIntoZone(leftZone)){
                    this.newX=this.newX-game.standardWidth;
                    this.explodedZones.push(leftZone);
                    movedLeft=true;
                    if(leftZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                        stopMovingLeft=true;
                    }
                }
            }
            if(!stopMovingRight && this.newX+(this.newWidth+(movedLeft?1:0)*game.standardWidth)+game.standardWidth/2<game.worldWidth){
                var rightZone = util.getZoneAtPoint(this.newX+(this.newWidth+(movedLeft?1:0)*game.standardWidth)+game.standardWidth/2,this.y);
                if(this.canExplodeIntoZone(rightZone)){
                    this.newWidth+=(movedLeft?2:1)*game.standardWidth;
                    this.explodedZones.push(rightZone);
                    if(rightZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                        stopMovingRight = true;
                    }
                }
            }
            if(!stopMovingTop && this.newY-game.standardHeight/2>0){
                var topZone = util.getZoneAtPoint(this.x,this.newY-game.standardHeight/2);
                if(this.canExplodeIntoZone(topZone)){
                    this.newY=this.newY-game.standardHeight;
                    this.explodedZones.push(topZone);
                    movedUp=true;
                    if(topZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                        stopMovingTop=true;
                    }
                }
            }
            if(!stopMovingBottom && this.newY+(this.newHeight+(movedUp?1:0)*game.standardHeight)+game.standardHeight/2<game.worldHeight){
                var bottomZone = util.getZoneAtPoint(this.x,this.newY+(this.newHeight+(movedUp?1:0)*game.standardHeight)+game.standardHeight/2);
                if(this.canExplodeIntoZone(bottomZone)){
                    this.newHeight+=(movedUp?2:1)*game.standardHeight;
                    this.explodedZones.push(bottomZone);
                    if(bottomZone.hasType(GAMEUNITTYPE.FAKEWALL)){
                        stopMovingBottom=true;
                    }
                }
            }
        }
        var horizEffect=false;
        if(this.newX!=this.x || this.newWidth!=game.standardWidth){
            if(!game.headless){
                var horiz = new Fx.Morph(this.HTMLIncarnation, {
                    duration:game.bombEffectDuration
                    });
                horiz.addEvent('onComplete',function(){
                    this.removeAshes();
                }.bind(this));
                horiz.start({
                    'width': [game.standardWidth,this.newWidth],
                    'left': [this.x,this.newX]
                });
            } else {
            // java code that waits game.bombEffectDuration and then calls removeAshes() on the objectId from nature [param this.id]
            }
            horizEffect=true;
        }
        var verticalEffect=false;
        if(this.newY!=this.y || this.newHeight!=game.standardHeight){
            if(game.headless){
            // java code that waits game.bombEffectDuration and then calls removeAshes() on the objectId from nature [param this.id]
            // mind the horiz one, not to callremoveAshes twice
            } else{
                this.clonedBomb = this.HTMLIncarnation.clone(false);
                this.clonedBomb.injectBefore(this.HTMLIncarnation);
                var vertical = new Fx.Morph(this.clonedBomb, {
                    duration:game.bombEffectDuration
                    });
                if(!horizEffect)
                    vertical.addEvent('onComplete',function(){
                        this.removeAshes();
                    }.bind(this));
                vertical.start({
                    'height': [game.standardHeight,this.newHeight],
                    'top': [this.y,this.newY]
                });
            }
            verticalEffect=true;
        }
        if(!horizEffect && !verticalEffect){
            this.terminate();
        }
    },
    terminate: function(){
        if($defined(this.clonedBomb)){
            this.clonedBomb.dispose();
            this.clonedBomb.destroy();
        }
        if(!game.headless)
            $clear(this.cancelFlag);
        util.removeElementFromArray(this,game.activeBombs);
        this.parent();
    },
    removeAshes: function(){
        this.terminate();
        this.hero.returnBomb();
        var heroHit=false;
        // instead of iterating through all heroes and walls i must add +1 to exploded zones in order to cover for the situations
        // when the hero had his right hand into the explosion, but the left hand and the left side of its div are into the zone
        // next to the explosion. this must be done in all 4 directions , and add it to the iteration
        // only when zone does not have a wall or other impossible situation.
        for(var i=0;i<this.explodedZones.length;i++){
            var elements=this.explodedZones[i].getElements();
            for(var k=0;k<elements.length;k++){
                if(elements[k].type!=GAMEUNITTYPE.BOMB && !(elements[k].type==GAMEUNITTYPE.GOODY && elements[k].subtype==GOODYSUBTYPE.EXIT))
                    elements[k].terminate();
                if(elements[k]==game.hero)
                    heroHit=true;
            }
        }
        if($defined(game.hero) && !heroHit){
            if(this.newX!=this.x || this.newWidth!=game.standardWidth){
                if(!(game.hero.x+game.standardWidth<=this.newX || game.hero.x>=this.newX+this.newWidth || game.hero.y+game.standardHeight<=this.oldY || game.hero.y>=this.oldY+game.standardHeight))
                    game.hero.terminate();
            }
        }
        if($defined(game.hero) && !heroHit){
            if(this.newY!=this.y || this.newHeight!=game.standardHeight){
                if(!(game.hero.y+game.standardHeight<=this.newY || game.hero.y>=this.newY+this.newHeight || game.hero.x+game.standardWidth<=this.oldX || game.hero.x>=this.oldX+game.standardWidth))
                    game.hero.terminate();
            }
        }
        for(var i=0;i<game.heroes.length;i++){
            var hero = game.heroes[i];
            if($defined(hero)){
                if(this.newX!=this.x || this.newWidth!=game.standardWidth){
                    if(!(hero.x+game.standardWidth<=this.newX || hero.x>=this.newX+this.newWidth || hero.y+game.standardHeight<=this.oldY || hero.y>=this.oldY+game.standardHeight))
                        hero.terminate();
                }
            }
            if($defined(hero)){
                if(this.newY!=this.y || this.newHeight!=game.standardHeight){
                    if(!(hero.y+game.standardHeight<=this.newY || hero.y>=this.newY+this.newHeight || hero.x+game.standardWidth<=this.oldX || hero.x>=this.oldX+game.standardWidth))
                        hero.terminate();
                }
            }
        }
    }
});
    